<?php


namespace app\common\util;

class WeChatAppUtil
{
    //统一下单请求接口
    public static $payUrl = 'https://api.mch.weixin.qq.com/pay/unifiedorder';

    // 支付成功回调地址
    public static $notify_url = "https://api.haopinyipai.com/0ad162451b1b1561081de08b815bad53/wechat-pay/notify";

    // 开发者的应用Id
    public static $appid_app = "";

    // 商户号
    public static $mchid = "";

    //商户号的是key  在微信商户平台上自己设定的api密钥 32位
    private $paternerkey= '';


    //发送curl post
    public function curl_post($url = '', $param = array(), $headers = array())
    {
        if (empty($url)) {
            return false;
        }
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);

        if ($headers) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        return $data;
    }

    //生成签名
    public function createSign($params)
    {
        //签名步骤一：按字典序排序参数
        ksort($params);
        $String = $this->formatBizQueryParaMap($params, false);

        //签名步骤二：在string后加入KEY
        $String = $String . "&key=" . $this->paternerkey;

        //签名步骤三：MD5加密
        $String = md5($String);

        //签名步骤四：所有字符转为大写
        $result_ = strtoupper($String);

        return $result_;

    }

    //按字典序排序参数
    public function formatBizQueryParaMap($params, $urlencode = false)
    {
        ksort($params);
        $buff = '';
        foreach ($params as $key => $val) {
            $buff .= $key . "=" . $val . "&";
        }
        $reqPar = substr($buff, 0, strlen($buff) - 1);
        return $reqPar;
    }

    //将数组转换为xml格式
    public function arrayToXml($arr)
    {
        $xml = "<xml>";
        foreach ($arr as $key => $val) {
            if (is_numeric($val))
                $xml .= '<' . $key . '>' . $val . '</' . $key . '>';
            else
                $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
        }

        $xml .= "</xml>";
        return $xml;
    }

    //把对象转成数组
    public function objectToarray($arr) {
        if(is_object($arr)) {
            $arr = (array)$arr;
        } if(is_array($arr)) {
            foreach($arr as $key=>$value) {
                $arr[$key] = $this->object_toarray($value);
            }
        }
        return $arr;
    }

    //随机字符串
    public function createNoncestr($length = 32)
    {
        $chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
        $str = '';
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }


    /**
     * 错误代码
     * @param  $code       服务器输出的错误代码
     * return string
     */
    public function error_code( $code ){
        $errList = array(
            'NOAUTH'                =>  '商户未开通此接口权限',
            'NOTENOUGH'             =>  '用户帐号余额不足',
            'ORDERNOTEXIST'         =>  '订单号不存在',
            'ORDERPAID'             =>  '商户订单已支付，无需重复操作',
            'ORDERCLOSED'           =>  '当前订单已关闭，无法支付',
            'SYSTEMERROR'           =>  '系统错误!系统超时',
            'APPID_NOT_EXIST'       =>  '参数中缺少APPID',
            'MCHID_NOT_EXIST'       =>  '参数中缺少MCHID',
            'APPID_MCHID_NOT_MATCH' =>  'appid和mch_id不匹配',
            'LACK_PARAMS'           =>  '缺少必要的请求参数',
            'OUT_TRADE_NO_USED'     =>  '同一笔交易不能多次提交',
            'SIGNERROR'             =>  '参数签名结果不正确',
            'XML_FORMAT_ERROR'      =>  'XML格式错误',
            'REQUIRE_POST_METHOD'   =>  '未使用post传递参数 ',
            'POST_DATA_EMPTY'       =>  'post数据不能为空',
            'NOT_UTF8'              =>  '未使用指定编码格式',
        );
        if( array_key_exists( $code , $errList ) ){
            return $errList[$code];
        }
    }
}